package ferp.center.server.entity;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Text;
import ferp.center.network.response.ResponseConfigGet;
import ferp.center.server.Action;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

/**
 * User: igorgok
 * Date: 12/9/12 2:04 PM
 */

@Entity
public class Config implements Serializable
{
  public Config() {}
/*
  public Config(Nen nen, int bidnn_tss, int bidnn_hls, float bidnn_error, long bidnn_time) throws IOException
  {
    this.bidnn_enabled = true;
    this.bidnn_threshold = ResponseConfigGet.DEFAULT_THRESHOLD;

    set(nen, bidnn_tss, bidnn_hls, bidnn_error, bidnn_time);
  }

  // copy some values from the old configuration
  public Config(Config old, Nen nen, int bidnn_tss, int bidnn_hls, float bidnn_error, long bidnn_time) throws IOException
  {
    this.bidnn_enabled = old.bidnn_enabled;
    this.bidnn_threshold = old.bidnn_threshold;

    this.levels_enabled = old.levels_enabled;
    this.levels_data = old.levels_data == null ? new Text(Action.gson().toJson(new Levels())) : old.levels_data;

    set(nen, bidnn_tss, bidnn_hls, bidnn_error, bidnn_time);
  }
*/

  public Config(Config old)
  {
    this.bidnn_id = old.getBidNNID();
    this.bidnn_enabled = old.bidnn_enabled;
    this.bidnn_threshold = old.bidnn_threshold;
    this.bidnn_hls = old.bidnn_hls;
    this.bidnn_setup = old.bidnn_setup;
    this.bidnn_tss = old.bidnn_tss;
    this.bidnn_error = old.bidnn_error;
    this.bidnn_json = old.bidnn_json;
    this.bidnn_data = old.bidnn_data;
    this.bidnn_size = old.bidnn_size;
    this.bidnn_time = old.bidnn_time;
    this.levels_enabled = old.levels_enabled;
    this.levels_data = old.levels_data;
    this.ad = old.ad;

    // now
    setDate();
  }

/*
  public void set(Nen nen, int tss, int hls, float error, long time)
  {
    String json = Action.gson().toJson(nen);

    this.bidnn_hls = hls;
    this.bidnn_tss = tss;
    this.bidnn_error = error;
    this.bidnn_time = time;

    // save NN state as JSON
    // this occupies more space in the DB, but saves on the network traffic
    this.bidnn_json = new Text(json);
    this.bidnn_size = json.length();
    // now
    this.created = new Date();
  }
*/

  public void resetKey() {key = null;}
  public long getId() {return key.getId();}
  //public Date getCreated() {return created;}
  public void setDate() {created = new Date();}

  public void setBidNNID(long id) {bidnn_id = id;}
  public long getBidNNID() {return bidnn_id == null ? 0 : bidnn_id;}
  public boolean getBidNNEnabled() {return bidnn_enabled;}
  public void setTrainingSetSize(int tss) {bidnn_tss = tss;}
  public int getTrainingSetSize() {return bidnn_tss;}
  public void setBidNNThreshold(int threshold) {bidnn_threshold = threshold;}
  public int getBidNNThreshold() {return bidnn_threshold;}
  public String getBidNNJson() {return bidnn_json.getValue();}
  public void setBidNNSetup(String setup) {bidnn_setup = setup;}
  public void setBidNNData(String data) {bidnn_data = new Text(data);}
  public String getBidNNData() {return bidnn_data.getValue();}
  public void setBidNNTime(long time) {bidnn_time = time;}

  public boolean getLevelsEnabled() {return levels_enabled;}
  public String getLevelsData() {return levels_data.getValue();}

  public boolean getMessagesEnabled() {return true;} // messages_enabled;}

  public void setAd(ResponseConfigGet.Ad ad) {this.ad = new Text(Action.gson().toJson(ad));}
  public ResponseConfigGet.Ad getAd()
  {
    if (ad == null)
      return new ResponseConfigGet.Ad();

    return Action.gson().fromJson(ad.getValue(), ResponseConfigGet.Ad.class);
  }

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Key key;

  @Temporal(TemporalType.DATE)
  private Date created;

  // bidding NN related data
    // id
  private Long bidnn_id;
    // enable local NNs (change value manually to disable)
  private boolean bidnn_enabled;
    // bid contributor's minimal ratio between total score and total played pool
  private int bidnn_threshold;
    // guessed hidden layer size
  private int bidnn_hls;
    // NN configuration: nhl:hls1,...
  private String bidnn_setup;
    // training set size
  private int bidnn_tss;
    // error
  private float bidnn_error;
    // NN configuration in JSON format
  private Text bidnn_json;
    // new bid NN compressed data
  private Text bidnn_data;
    // configuration text size
  private int bidnn_size;
    // training time
  private long bidnn_time;

  // opponent levels configuration
  private boolean levels_enabled;
  private Text levels_data;

  // AD configuration
  private Text ad;

  // messages
  //private boolean messages_enabled = true;

  @Version
  private int version;
}
